package com.cn.learn.jpa.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cn.learn.jpa.domain.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @ClassName UserMapper
 * @Description 用户表的代理接口
 * @Author JiJiang
 * @Date 2022/9/14 14:56
 * @Version 1.0
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {


    @Select("select * from user where id = #{id}")
    User selectById(User user);


    /**
     * @MethodName selectUsersByConditions
     * @Description 根据条件查询所有用户信息
     * @Param [user]
     * @Return com.xn.wanxi.bootlearnstep7.domain.entity.User
     **/
    @Select({"<script>"
            , "select * from user "
            , "<where>"
            , "<if test=\"name !=null and name !='' \">"
            , "and name like concat('%',#{name},'%')"
            , "</if>"
            , "</where>"
            , "</script>"
    })
    List<User> selectUsersByConditions(User user);


    /**
     * @MethodName insertAUser
     * @Description 插入一条用户记录
     * @Param [user]
     * @Return int
     **/
    @Insert({"<script>"
            , "insert into user (id,name,birth_date)"
            , "values( null,"
            , "<if test =  \"name == null or name == ''\">"
            , "'无名',"
            , "</if>"

            , "<if test =  \"name != null and name != ''\">"
            , "#{name},"
            , "</if>"

            , "<if test =  \"birthDate == null\">"
            , "date_format(now(),'%Y-%m-%d %H:%i:%S')"
            , "</if>"

            , "<if test =  \"birthDate != null\">"
            , "#{birthDate}"
            , "</if>"

            , ")"
            , "</script>"
    })
    int insertAUser(User user);


    /**
     * @MethodName deleteById
     * @Description 根据id删除一条用户记录
     * @Param [user]
     * @Return int
     **/
    @Delete("delete from user where id = #{id}")
    int deleteById(User user);


    /**
     * @MethodName updateById
     * @Description 更新一条用户记录
     * @Param [user]
     * @Return int
     **/
    @Update({"<script>"
            , "update user set id = id "

            , "<if test=\"name !=null and name !='' \">"
            , ",name= #{name}"
            , "</if>"

            , "<if test=\"birthDate !=null  \">"
            , ",birth_date= #{birthDate}"
            , "</if>"

            , "where id = #{id}"
            , "</script>"
    })
    int updateById(User user);


    @Select({"<script>"
            , "select count(id) from user "
            , "<where>"
            , "<if test=\"name !=null and name !='' \">"
            , "and name like concat('%',#{name},'%')"
            , "</if>"
            , "</where>"
            , "</script>"
    })
    int selectTotalCountByConditions(User user);



    List<User> seAll(User user);

}
